﻿using System;
using System.Collections.Generic;
using System.Linq;
using Core;
using Core.Battle;
using Core.Battle.EntityController;

public class Program
{
    public static void Main(string[] args)
    {
        Abilities.LoadAbilities();

        IEntity player = new Player("ZerAx");
        IEntity dummy = new Dummy();
        IEntity dummy2 = new Dummy();

        player.Controller = new TerminalController(player);
        dummy.Controller = new DummyController(dummy);
        dummy2.Controller = new DummyController(dummy2);

        var currentSide = new List<IEntity>();
        currentSide.Add(player);

        var opponentSide = new List<IEntity>();
        opponentSide.Add(dummy);
        opponentSide.Add(dummy2);
        

        var chain = new List<Ability>();

        var running = true;

        while (running)
        {
            // Round Begin
            chain.Clear();
            foreach (var entity in currentSide.Concat(opponentSide))
            {
                foreach (var effect in entity.Effects)
                {
                    effect.OnRoundBegin();
                }
            }

            Console.WriteLine("选择组合的元素([W]水, [G]草, [A]气, [S]释放)");
            // Collect Player Inputs
            Ability pab = null;
            // Wait for player ability
            while (pab == null) pab = player.Controller.GetAbility(opponentSide);
            foreach (var cur in currentSide)
            {
                foreach (var effect in cur.Effects)
                {
                    effect.OnAbilityApply(ref pab);
                }
            }
            chain.Add(pab);

            // Collect Enemy Inputs
            foreach (var opponent in opponentSide)
            {
                Ability oab = null;
                while (oab == null) oab = opponent.Controller.GetAbility(currentSide);
                foreach (var effect in opponent.Effects)
                {
                    effect.OnAbilityApply(ref oab);
                }
                chain.Add(oab);
            }

            // Apply Abilities
            chain.Sort((Ability a, Ability b) => b.Speller.Speed - a.Speller.Speed);

            foreach (var ability in chain)
            {
                ability.Spell();
                foreach (var effect in ability.Speller.Effects)
                {
                    effect.OnAbilitySpell(ability);
                }
            }

            // Round End
            foreach (var entity in currentSide.Concat(opponentSide))
            {
                foreach (var effect in entity.Effects)
                {
                    effect.OnRoundEnd();
                }
            }

            Console.WriteLine();
        }
    }
}